package cn.dnui_wwt524.dnuifood_16110100524.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import cn.dnui_wwt524.dnuifood_16110100524.R;

public class BaseFragment extends Fragment {
    private SharedPreferences sp;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    protected Context context;

    public BaseFragment(){
        context=getActivity();
    }

    public String getUsers_id(){
        sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        return sp.getString("userid","");
    }

    public void setLayout_file(int layout_file) {

    }

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        childManager=getChildFragmentManager();
    }

    protected void addFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.add(R.id.viewPager,fragment);
        transaction.commit();
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    protected  void replaceFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.replace(R.id.viewPager,fragment);
        transaction.commit();
    }

    protected void removeFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
    }
}
